using System.Collections.Generic;
using System.Threading.Tasks;
using JoyfulHomeJourney.BackEnd.Infrastructure.Atrributes;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Requests.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.EFCore;
using JoyfulHomeJourney.BackEnd.Server.EFCore;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Requests;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Responses;
using JoyfulHomeJourney.BackEnd.Server.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace JoyfulHomeJourney.BackEnd.Server.Instances
{
/// <summary>
/// 一个类包含了服务类所需的所有仓库表的实例，仅提供给该服务类使用，请将所有服务类所需的仓库表的实例都放在该类中。
/// 仓库表的实例指提供了操作数据库指定表的所有方法的对象
/// </summary>
	internal class ShoppingCarsEF
	{
		public ShoppingCarsEF(DbContext dbContext)
		{
			shoppingCarsServerEF = new ShoppingCarsServerEF(dbContext);
			sysProductsServerEF = new SysProductsServerEF(dbContext);
		}
		public ShoppingCarsServerEF shoppingCarsServerEF { get; set; }
		public SysProductsServerEF sysProductsServerEF { get; set; }
	}
	[InjectClass]
	public class ShoppingCarsServer : IShoppingCarsServer
	{
		private readonly ShoppingCarsEF _shoppingCarsEF;//包含所有所需仓库表实现的对象
		// 请不要修改该构造方法,也无需添加属性，而应该在上面的EF类中添加属性和构造函数赋值语句
		public ShoppingCarsServer(DbContext dbContext)
		{
			_shoppingCarsEF = new ShoppingCarsEF(dbContext);
		}
		public async Task<ResponseApiDto> AddAsync(RequestShoppingCarsDto dto,string aid)
		{
			if (await _shoppingCarsEF.shoppingCarsServerEF.Query(d => d.ScAccountId ==aid).CountAsync() + 1 > 100)
			{
				return ResponseApiDto.Fail("购物车最多添加一百个商品");
			}
			var shoppingCar = dto.ToCreateEntity();
			shoppingCar.ScAccountId = aid;
            return ResponseApiDto.Create(await _shoppingCarsEF.shoppingCarsServerEF.AddAsync(shoppingCar));
		}
		public async Task<ResponseApiDto> DeleteAsync(RequestShoppingCarsDto dto, string aid)
		{
			return ResponseApiDto.Create(await _shoppingCarsEF.shoppingCarsServerEF.DeleteAsync(dto.ToDeleteEntity()));
		}
		public async Task<ResponseApiDto> UpdateAsync(RequestShoppingCarsDto dto, string aid)
		{
			return ResponseApiDto.Create(await _shoppingCarsEF.shoppingCarsServerEF.UpdateAllProperNotNullAsync(dto.ToUpdateEntity()));
		}
		public async Task<ResponseApiDto> PaginationAsync(int page, int limit,string aid)
		{
			int total = 0;
			List<ResponseShoppingCarsDto> data = new List<ResponseShoppingCarsDto>();
			List<ShoppingCars> shoppingCarss = await _shoppingCarsEF.shoppingCarsServerEF.Pagination1(d=>d.ScAccountId==aid,false, d => d.ScProductId, out total, page, limit).ToListAsync();
			foreach (var shoppingCars in shoppingCarss)
			{
				var product = await _shoppingCarsEF.sysProductsServerEF.Query(d => d.PId == shoppingCars.ScProductId).SingleOrDefaultAsync();
				data.Add(product != null ? shoppingCars.ToResponseDto(product) : null);
			}
			return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { total=total,data=data});
		}

		public async Task<ResponseApiDto> ReduceCount(string id)
		{
            var shoppingCar = await _shoppingCarsEF.shoppingCarsServerEF.Query(d => d.ScId == id).SingleOrDefaultAsync();
            shoppingCar.ScCount -= 1;
            return ResponseApiDto.Create(await _shoppingCarsEF.shoppingCarsServerEF.UpdateAllProperAsync(shoppingCar));
        }
        public async Task<ResponseApiDto> RiseCount(string id)
        {
			var shoppingCar=await _shoppingCarsEF.shoppingCarsServerEF.Query(d => d.ScId == id).SingleOrDefaultAsync();
			shoppingCar.ScCount += 1;
			return ResponseApiDto.Create(await _shoppingCarsEF.shoppingCarsServerEF.UpdateAllProperAsync(shoppingCar));
        }
    }
}
